import os
import numpy as np
import GPyOpt

def f(x):
    x = x[0]
    complete_x = np.array([x[0], 0.01, x[1], 0.01, x[2], x[3], 0.0125, x[4], 0.0125, x[5]])
    # size = np.size(x, 0)

    file_name = 'ansys_file/fr_sim.txt'
    bak_name = 'ansys_file/.%fr_sim.txt.bak'

    original_file = file(file_name, 'rb')
    bak_file = file(bak_name, 'wb')

    line_num = 0
    for line in original_file.xreadlines():
        if line_num > 9 and line_num < 20:
            bak_file.write('SECTYPE,' + str(line_num - 9) +
                           ',BEAM,CSOLID,,0$SECOFFSET,CENT$SECDATA,' + str(complete_x[line_num - 10]) +
                           ',8,8,0,0,0,0,0,0,0,0,0\n')
        else:
            bak_file.write(line)
        line_num += 1
    bak_file.close()
    original_file.close()

    old_file_name = 'ansys_file/old_fr_sim.txt'
    if os.path.exists(old_file_name):
        os.remove(old_file_name)
    os.rename(file_name, old_file_name)
    os.rename(bak_name, file_name)

    os.system("\"D:\\tools_file\\ansys\ANSYS Inc\\v130\\ansys\\bin\\winx64\\ansys130.exe\" -b "
              "-i F:\\sevn\\py_workspace\\simulation_optimizer\\ansys_file\\fr_sim.txt "
              "-o F:\\sevn\\py_workspace\\simulation_optimizer\\ansys_file\\123.txt")

    critical_speed = np.loadtxt('critical_speed.TXT')
    strain_energy1 = np.loadtxt('strain_energy1.TXT')
    strain_energy5 = np.loadtxt('strain_energy5.TXT')
    strain_energy9 = np.loadtxt('strain_energy9.TXT')

    original_critical_speed = np.array([5673, 12186, 27716])
    original_strain_energy1 = np.array([47.56, 0.37, 6.93, 0.24, 44.9])
    original_strain_energy5 = np.array([22.83, 0.33, 5.12, 49.42, 22.3])
    original_strain_energy9 = np.array([67.23, 3.02, 29.73, 0.02, 0.01])

    f_1 = np.linalg.norm((critical_speed - 0.4 * original_critical_speed) / (0.4 * original_critical_speed))
    energy_dif_1 = np.linalg.norm((strain_energy1 - original_strain_energy1) / original_strain_energy1)
    energy_dif_5 = np.linalg.norm((strain_energy5 - original_strain_energy5) / original_strain_energy5)
    strain_dif_9 = np.linalg.norm((strain_energy9 - original_strain_energy9) / original_strain_energy9)

    return f_1 + (energy_dif_1 + energy_dif_5 + strain_dif_9) / 3


if __name__ == '__main__':
    bounds = [{'name': 'var_1', 'type': 'continuous', 'domain': (0.005, 0.0095)},
              {'name': 'var_3', 'type': 'continuous', 'domain': (0.0105, 0.011)},
              {'name': 'var_5', 'type': 'continuous', 'domain': (0.005, 0.0095)},
              {'name': 'var_6', 'type': 'continuous', 'domain': (0.0095, 0.012)},
              {'name': 'var_8', 'type': 'continuous', 'domain': (0.0135, 0.0145)},
              {'name': 'var_10', 'type': 'continuous', 'domain': (0.005, 0.012)}]
    myBopt = GPyOpt.methods.BayesianOptimization(f=f,
                                                 domain=bounds,
                                                 acquisition_type='EI',
                                                 exact_feval=True)
    max_iter = 50
    eps = 10e-6
    myBopt.run_optimization(max_iter=max_iter, eps=eps)
    print myBopt.x_opt
    print myBopt.fx_opt
